#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int main( )
{
    int t, cas, n, i, j;
    char s[ 100 ], minc, tmp;
    bool flag;
    scanf("%d", &t);
    while ( t-- )
    {
        scanf("%d%s", &cas, s);
        n = strlen( s );
        for ( i = n - 2; i >= 0; i-- )
        {
            flag = true;
            minc = 0;
            for ( j = i + 1; j < n; j++ )
                if ( s[ j ] > s[ i ] && ( flag || s[ j ] < s[ minc ] ) )
                {
                    flag = false;
                    minc = j;
                }
            if ( !flag )
                break;
        }
        if ( i < 0 )
            printf("%d BIGGEST\n", cas);
        else
        {
            tmp = s[ i ];
            s[ i ] = s[ minc ];
            s[ minc ] = tmp;
            sort( s + i + 1, s + n);
            printf("%d %s\n", cas, s);
        }
    }
    return 0;
}